//
//  ACRefreshViewController.m
//  ACFramework
//
//  Created by caoyq on 2018/5/23.
//

#import "ACRefreshViewController.h"
#import "MJRefresh.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
#import "ACBaseViewModel.h"
#import "ACMacro.h"

@interface ACRefreshViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

/** uicollectionview 和uitableview的替身 */
@property (nonatomic, strong) UIScrollView *tempScrollView;

@end

@implementation ACRefreshViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - Refresh

- (void)addHeaderRefresh: (BOOL)headerRefresh FooterRefresh: (BOOL)footerRefresh
{
    if (headerRefresh) {
        self.tempScrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        self.tempScrollView.mj_header.automaticallyChangeAlpha = YES;
        [self autoHeaderRefresh];
    }
    if (footerRefresh) {
        self.tempScrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        self.tempScrollView.mj_footer.automaticallyChangeAlpha = YES;
    }
}

- (void)autoHeaderRefresh
{
    [self.tempScrollView.mj_header beginRefreshing];
}

///下拉刷新
- (void)loadNewData
{
    if ([self.tempScrollView.mj_footer isRefreshing]) {
        [self.tempScrollView.mj_footer endRefreshing];
    }
    [self loadData:YES];
}

///上拉加载更多
- (void)loadMoreData
{
    if ([self.tempScrollView.mj_header isRefreshing]) {
        [self.tempScrollView.mj_header endRefreshing];
    }
    [self loadData:NO];
}

- (void)loadData: (BOOL)isHeader
{
    ACWeakSelf
    [self.viewModel loadData:isHeader success:^(BOOL haveMoreData) {
        [weakSelf didFinishRefresh:isHeader haveMoreData:haveMoreData];
    } failure:^{
        [weakSelf didFinishWithError];
    }];
}

///请求数据成功后对mj_header、mj_footer做些收尾
- (void)didFinishRefresh: (BOOL)isHeader haveMoreData: (BOOL)haveMoreData
{
    ACWeakSelf
    dispatch_async(dispatch_get_main_queue(), ^{
        ACStrongSelf
        if (self.tempScrollView.emptyDataSetSource == nil) {
            self.tempScrollView.emptyDataSetSource = self;
            self.tempScrollView.emptyDataSetDelegate = self;
        }
        
        if (isHeader) {
            [self.tempScrollView.mj_header endRefreshing];
            
            if (self.isBanRefreshBehindFirst) {
                self.tempScrollView.mj_header.hidden = YES;
            }
            
            if (haveMoreData) { //下拉刷新后面还有值
                if (self.tempScrollView.mj_footer.hidden) {  //之前mj_footer是影藏
                    self.tempScrollView.mj_footer.hidden = NO;
                }
                if (self.tempScrollView.mj_footer.state == MJRefreshStateNoMoreData) { //之前显示的没有更多数据
                    [self.tempScrollView.mj_footer resetNoMoreData];
                }
            }else {
                self.tempScrollView.mj_footer.hidden = YES;
            }
            if (!self.dataSource.count) {
                [self.tempScrollView reloadEmptyDataSet];
            }
            
        }else {
            if (haveMoreData) {
                [self.tempScrollView.mj_footer endRefreshing];
            }else {
                [self.tempScrollView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        
        [self reloadData];
    });
}

///请求数据失败之后的处理
- (void)didFinishWithError
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.tempScrollView.mj_header) {
            [self.tempScrollView.mj_header endRefreshing];
        }
        if (self.tempScrollView.mj_footer) {
            [self.tempScrollView.mj_footer endRefreshing];
        }
    });
}

- (void)reloadData
{
    if (self.tableView) {
        [self.tableView reloadData];
    }else if (self.collectionView) {
        [self.collectionView reloadData];
    }
}

- (void)showRefreshHeader
{
    self.tempScrollView.mj_header.hidden = NO;
}

#pragma mark - Custom EmptyDataSet

- (UIImage *)imageForEmptyList
{
    return [UIImage new];
}

- (NSAttributedString *)attributeStringForEmptyList
{
    return nil;
}

- (NSString *)stringForEmptyList
{
    return @"没有数据";
}

#pragma mark - DZNEmptyDataSetSource

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSAttributedString *attributeStr = [self attributeStringForEmptyList];
    if (attributeStr) {
        return attributeStr;
    }
    
    NSString *str = [self stringForEmptyList];
    attributeStr = [[NSAttributedString alloc] initWithString:str
                                                   attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0f], NSForegroundColorAttributeName : [UIColor blackColor]}];
    return attributeStr;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [self imageForEmptyList];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    CGPoint point = [self.tempScrollView convertPoint:CGPointZero toView:[UIApplication sharedApplication].keyWindow];
    return [UIScreen mainScreen].bounds.size.height / 2.0f - point.y - self.tempScrollView.frame.size.height / 2.0;
}

#pragma mark - DZNEmptyDataSetDelegate

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView
{
    [UIView animateWithDuration:0.5 animations:^{
        scrollView.contentOffset = CGPointZero;
    }];
}

#pragma mark - getter

- (UIScrollView *)tempScrollView
{
    if (self.tableView) {
        return self.tableView;
    }else if (self.collectionView) {
        return self.collectionView;
    }
    return nil;
}

@end
